import $api from "./api.js"
import host from "./host.js"
import store from '@/store'

const appTimeout = 300000;
const uploadImageMaxSize = 5242880;
//默认请求应答格式，错误码、错误消息、数据、总数据长
const responseFormat = {
	errCode: "code",
	errMsg: "message",
	data: "data",
	count: "count"
}
//默认错误码表，成功、业务错误（带服务器消息）、登录超时
const errFormat = {
	success: 0,
	serviceErr: 1,
	loginTimeout: -2,
}

/*
promise化，保留obj.success、obj.fail的执行
noPromiseCallback 取消promise完成后success、fail的默认执行。默认false
promise.cancel() 若promise未完成。则取消promise，返回reject
*/
const toPromise = (callback)=>{
	return (obj = {}) => {
		let undo = false;
		let callbackReturn;
		let pro = new Promise((resolve, reject) => {
			let {success, fail} = obj;
			//重置obj的回调，除执行原本的回调，另设置promise状态
			obj.success = (...args) => {
				if(undo){//判断取消操作
					reject({ msg: "操作已过期", type: "cancel" })
				}else{
					if(success && !obj.noPromiseCallback){
						success(...args)
					}
					resolve(...args)
				}
			};
			obj.fail = (...args) => {
				if(undo){//判断取消操作
					reject({ msg: "操作已过期", type: "cancel" })
				}else{
					if(fail && !obj.noPromiseCallback){
						fail(...args)
					}
					reject(...args)
				}
			};
	        // promise执行
	        callbackReturn = callback(obj)
	    })
		pro["cancel"] = function(){
			undo = true;
		}
		pro["handle"] = callbackReturn;
		return pro
	}
}

const gotoLogin = () => {
	$api.msg('您未登录或已超时，请先登录');
	//可能存在多个请求超时，需节流
	if(store.state.user.goLoginTimer){
		return
	}
	//跳转登录页
	store.state.user["goLoginTimer"] = setTimeout(function(){
		uni.redirectTo({
			url:"/pages/login/login",
			complete:()=>{
				store.state.user["goLoginTimer"] = null
			}
		})
	}, $api.msgDuration);
}
//请求前遮罩打开
const openLoading = (obj) => {
	//使用自定义loading组件
	let pages = getCurrentPages();
	let currPage = pages[pages.length-1];
	if(currPage){
		currPage.$vm["vi_loading"] = true;
		//如果需要加载遮罩，不给点击页面
		if(obj.loadingMask){
			currPage.$vm["vi_loadingMask"] = true;
		}
	}
	// uni.showLoading();
}
//请求后遮罩关闭
const closeLoading = (obj) => {
	//使用自定义loading组件
	let pages = getCurrentPages();
	let currPage = pages[pages.length-1];
	if(currPage){
		currPage.$vm["vi_loading"] = false;
		currPage.$vm["vi_loadingMask"] = false;
	}
	// uni.hideLoading();
}

/*
一般请求
obj.resCheck 控制是否对请求结果进行前置校验（包括开启数据格式化）不校验则返回完整数据，以用户设置优先。默认false
obj.resFull 控制是否返回完整数据，开启结果校验后有效，以用户设置优先。默认false
*/
const nquest = (obj = {}, lock) => {
	if(lock){//执行锁，当次请求完成前不能再次执行。传入外部对象 {value:false, msg:"正在请求中，请稍后", once:false}。once为单步锁，请求成功后即锁死
		if(lock.value){
			if(lock.msg){
				$api.msg(lock.msg, "warning");
			}
			return Promise.reject({ msg: "请求锁定", type: "locking", from:"nquest", source:obj});
		}
		lock["value"] = true
	}
	obj["noPromiseCallback"] = true;
	if(!obj.noLoading){
		openLoading(obj);
	}
	//请求数据
	let questData = {
		url: obj.url,
		data: obj.data,
		fields: obj.fields || [],
		method: obj.method || 'POST',
		header: Object.assign({
			'content-type':'application/json'
		}, obj.header),
		timeout: obj.timeout || 10000,
		dataType: obj.dataType || 'json',
		responseFormat: responseFormat,
		errFormat: errFormat,
		dataFormat: "",
		paramUrl: obj.paramUrl,
		resCheck: false,
		resFull: false,
	}
	//合并接口独立设置
	if(obj.port){
		let portObj;
		let portHeader;
		if($api.isString(obj.port)){
			portObj = host.port[obj.port] ||{}
		}else if($api.isObject(obj.port)){
			portObj = obj.port
		}
		//单独提出header合并处理
		if(portObj.header){
			portHeader = Object.assign({}, questData.header, portObj.header);
		}
		$api.objMerge(questData, portObj);
		if(portHeader){
			questData["header"] = portHeader
		}
	}
	//前置校验
	if(obj.hasOwnProperty("resCheck")){
		questData["resCheck"] = obj.resCheck
	}
	//完整数据
	if(obj.hasOwnProperty("resFull")){
		questData["resFull"] = obj.resFull
	}
	if(!questData.url){//无url，返回错误
		if(!obj.noLoading){
			closeLoading(obj);
		}
		//解锁
		if(lock){
			lock["value"] = false
		}
		return Promise.reject({ msg: "url为空", type: "urlErr", from:"nquest", source:obj});
	}
	//参数强制写入url，分两种情况，1、问号开头的参数列表；2、直接连接url
	if(questData.paramUrl){
		if(questData.paramUrl == "splice"){//直接拼接
			questData.url = questData.url + questData.data.data
		}else{
			questData.url = $api.stringParams(questData.data, questData.url);
		}
		delete questData.data;
	}
	//请求字段过滤
	if($api.isArray(questData.fields) && questData.fields.length){
		Object.keys(questData.data).map((item)=>{
			if(!questData.fields.includes(item)){
				delete questData.data[item]
			}
		});
	}
	//开始请求，得到请求对象
	let questPromise = toPromise(uni.request)(questData);
	let resPromise = questPromise.then((resData)=>{//数据请求完成
		let errObj, showMsg;
		if(resData.statusCode == 200){//成功，返回包装内层作为原始数据
			let trueData = resData.data;
			if(!questData.resCheck){//无需结果校验（也不进行数据格式化），直接返回完整数据
				if(obj.success){
					obj.success(trueData);
				}
				//解锁
				if(lock&&!lock.once){
					lock["value"] = false
				}
				return Promise.resolve(trueData);
			}else{//结果校验
				let errCode = trueData[questData.responseFormat.errCode];
				let errCodeDic = questData.errFormat;
				if($api.isObject(trueData)){
					if(errCode == errCodeDic.success){//校验成功
						if(questData.dataFormat){//数据格式化，如果有
							dataFormat(trueData[questData.responseFormat.data], questData.dataFormat)
						}
						if(obj.success){
							if(questData.resFull){
								obj.success(trueData)
							}else{
								obj.success(trueData[questData.responseFormat.data])
							}
						}
						//解锁
						if(lock&&!lock.once){
							lock["value"] = false
						}
						if(questData.resFull){
							return Promise.resolve(trueData);
						}
						return Promise.resolve(trueData[questData.responseFormat.data]);
					}else if(errCode == errCodeDic.serviceErr){//业务错误，带服务器消息
						showMsg = trueData[questData.responseFormat.errMsg];
						errObj = { msg: "业务流错误", type: "serviceErr" };
					}else{
						showMsg = "服务器返回数据错误";
						errObj = { msg: "未定义的错误码或数据定义错误", type: "dataErr" };
					}
				}else{//结果为非校验格式
					showMsg = "服务器返回数据错误";
					errObj = { msg: "返回数据为非对象格式", type: "dataErr" };
				}
				if(errObj){
					errObj["response"] = trueData;
				}
			}
		}else{//失败，判断原因
			showMsg = "http请求异常";
			errObj = { msg: "http状态码异常", type: "httpCodeErr", code:resData.statusCode };
		}
		
		if(!errObj){//未进入成功流且未得到错误对象，检查以上代码是否正确
			showMsg = "http请求出错";
			errObj = { msg: "未定义的http错误，检查http核心文件代码！", type: "codeErr" };
		}
		errObj["from"] = "nquest";
		errObj["source"] = obj;
		if(!obj.noErrMsg){
			$api.msg(showMsg, "error");
		}
		if(obj.fail){
			obj.fail();
		}
		//解锁
		if(lock){
			lock["value"] = false
		}
		return Promise.reject(errObj);
	},(err)=>{//失败回调,不应该捕捉success内错误，不使用catch
		//解锁
		if(lock){
			lock["value"] = false
		}
		if(err && err.type == "cancel"){//取消操作，错误传递
			return Promise.reject(Object.assign(err, { from:"nquest", source: obj }));
		}else{
			if(!obj.noErrMsg){
				$api.msg('网络异常', "error");
			}
			if(obj.fail){
				obj.fail();
			}
			return Promise.reject({ from:"nquest", msg: "http请求失败", type: "httpErr", source: obj });
		}
	}).finally(()=>{
		if(!obj.noLoading){
			closeLoading(obj);
		}
		if(obj.complete){
			obj.complete();
		}
	});
	//请求取消
	resPromise["cancel"] = function(){
		questPromise.cancel();
		questPromise.handle.abort();
	}
	return resPromise
}

/*
登录人请求，校验token
obj.resCheck 控制是否对请求结果进行前置校验（包括开启数据格式化）不校验则返回完整数据，以用户设置优先。默认true
obj.resFull 控制是否返回完整数据，开启结果校验后有效，以用户设置优先。默认false
*/
const uquest = (obj = {}, lock) => {
	//获取本地token
	let token;
	try {
		token = uni.getStorageSync('user_token');
		if(!token){
			gotoLogin();
			return Promise.reject({ msg: "未登录", type: "unlogin" });
		}
	}catch(e){
	    gotoLogin();
		return Promise.reject({ msg: "未登录", type: "unlogin" });
	}
	if(lock){//执行锁，当次请求完成前不能再次执行。传入外部对象 {value:false, msg:"正在请求中，请稍后"}
		if(lock.value){
			if(lock.msg){
				$api.msg(lock.msg, "warning");
			}
			return Promise.reject({ msg: "请求锁定", type: "locking", from:"uquest", source:obj});
		}
		lock["value"] = true
	}
	obj["noPromiseCallback"] = true;
	if(!obj.noLoading){
		openLoading(obj);
	}
	//请求数据
	let questData = {
		url: obj.url,
		data: obj.data,
		fields: obj.fields || [],
		method: obj.method || 'POST',
		header: Object.assign({
			"content-type": "application/json",
			"Access-Token": token,
			//'AppKey': config.appkey 暂时没设AppKey
		}, obj.header),
		timeout: obj.timeout || 10000,
		dataType: obj.dataType || 'json',
		responseFormat: responseFormat,
		errFormat: errFormat,
		dataFormat: "",
		paramUrl: obj.paramUrl,
		resCheck: true,
		resFull: false,
	}
	//合并接口独立设置
	if(obj.port){
		let portObj;
		let portHeader;
		if($api.isString(obj.port)){
			portObj = host.port[obj.port] ||{}
		}else if($api.isObject(obj.port)){
			portObj = obj.port
		}
		//单独提出header合并处理
		if(portObj.header){
			portHeader = Object.assign({}, questData.header, portObj.header);
		}
		$api.objMerge(questData, portObj);
		if(portHeader){
			questData["header"] = portHeader
		}
	}
	//前置校验
	if(obj.hasOwnProperty("resCheck")){
		questData["resCheck"] = obj.resCheck;
	}
	//完整数据
	if(obj.hasOwnProperty("resFull")){
		questData["resFull"] = obj.resFull;
	}
	if(!questData.url){//无url，返回错误
		if(!obj.noLoading){
			closeLoading(obj);
		}
		//解锁
		if(lock){
			lock["value"] = false
		}
		return Promise.reject({ msg: "url为空", type: "urlErr", from:"uquest", source:obj});
	}
	//参数强制写入url，分两种情况，1、问号开头的参数列表；2、url直接拼接参数
	if(questData.paramUrl){
		if(questData.paramUrl == "splice"){//直接拼接
			questData.url = questData.url + questData.data.data
		}else{
			questData.url = $api.stringParams(questData.data, questData.url);
		}
		delete questData.data;
	}
	//请求字段过滤
	if($api.isArray(questData.fields) && questData.fields.length){
		Object.keys(questData.data).map((item)=>{
			if(!questData.fields.includes(item)){
				delete questData.data[item]
			}
		});
	}
	let questPromise = toPromise(uni.request)(questData);
	let resPromise = questPromise.then((resData)=>{//数据请求完成
		let errObj, showMsg;
		if(resData.statusCode == 200){//成功，返回包装内层作为原始数据
			let trueData = resData.data;
			if(!questData.resCheck){//无需结果校验（也不进行数据格式化），直接返回完整数据
				if(obj.success){
					obj.success(trueData);
				}
				//解锁
				if(lock&&!lock.once){
					lock["value"] = false
				}
				return Promise.resolve(trueData);
			}else{//结果校验
				let errCode = trueData[questData.responseFormat.errCode];
				let errCodeDic = questData.errFormat;
				if($api.isObject(trueData)){
					if(errCode == errCodeDic.success){//校验成功
						if(questData.dataFormat){//数据格式化，如果有
							dataFormat(trueData[questData.responseFormat.data], questData.dataFormat)
						}
						if(obj.success){
							if(questData.resFull){//完整数据
								obj.success(trueData)
							}else{
								obj.success(trueData[questData.responseFormat.data])
							}
						}
						//解锁
						if(lock&&!lock.once){
							lock["value"] = false
						}
						if(questData.resFull){
							return Promise.resolve(trueData);
						}
						return Promise.resolve(trueData[questData.responseFormat.data]);
					}else if(errCode == errCodeDic.serviceErr){//业务错误，带服务器消息
						showMsg = trueData[questData.responseFormat.errMsg];
						errObj = { msg: "业务流错误", type: "serviceErr" };
					}else if(errCode == errCodeDic.loginTimeout){//登录超时
						gotoLogin();
						return Promise.reject({ msg: "登录超时", type: "loginTimeout" });
					}else{
						showMsg = "服务器返回数据错误";
						errObj = { msg: "未定义的错误码或数据定义错误", type: "dataErr" };
					}
				}else{
					showMsg = "服务器返回数据错误";
					errObj = { msg: "返回数据为非对象格式", type: "dataErr" };
				}
				if(errObj){
					errObj["response"] = trueData;
				}
			}
		}else{//失败，判断原因
			showMsg = "http请求异常";
			errObj = { msg: "http状态码异常", type: "httpCodeErr", code:resData.statusCode };
		}
		
		if(!errObj){//未进入成功流且未得到错误对象，检查以上代码是否正确
			showMsg = "http请求出错";
			errObj = { msg: "未定义的http错误，检查http核心文件代码！", type: "codeErr" };
		}
		errObj["from"] = "uquest";
		errObj["source"] = obj;
		if(!obj.noErrMsg){
			$api.msg(showMsg, "error");
		}
		if(obj.fail){
			obj.fail();
		}
		//解锁
		if(lock){
			lock["value"] = false
		}
		return Promise.reject(errObj);
	},(err)=>{//失败回调,不应该捕捉success内错误，不使用catch
		//解锁
		if(lock){
			lock["value"] = false
		}
		if(err && err.type == "cancel"){//取消操作，错误传递
			return Promise.reject(Object.assign(err, { from:"uquest", source: obj }));
		}else{
			if(!obj.noErrMsg){
				$api.msg('网络异常', "error");
			}
			if(obj.fail){
				obj.fail();
			}
			return Promise.reject({ from:"uquest", msg: "http请求失败", type: "httpErr", source: obj });
		}
	}).finally(()=>{
		if(!obj.noLoading){
			closeLoading(obj);
		}
		if(obj.complete){
			obj.complete();
		}
	});
	//请求取消
	resPromise["cancel"] = function(){
		questPromise.cancel();
		questPromise.handle.abort();
	}
	return resPromise
}

/*
文件上传，校验token
*/
const upload = (obj = {}, lock) => {
	//获取本地token
	let token;
	try{
		token = uni.getStorageSync('user_token');
		if(!token){
			gotoLogin();
			return Promise.reject({ msg: "未登录", type: "unlogin" });
		}
	}catch(e){
	    gotoLogin();
		return Promise.reject({ msg: "未登录", type: "unlogin" });
	}
	if(lock){//执行锁，当次请求完成前不能再次执行。传入外部对象 {value:false, msg:"正在请求中，请稍后"}
		if(lock.value){
			if(lock.msg){
				$api.msg(lock.msg, "warning");
			}
			return Promise.reject({ msg: "请求锁定", type: "locking", from:"upload", source:obj});
		}
		lock["value"] = true
	}
	obj["noPromiseCallback"] = true;
	if(!obj.noLoading){
		openLoading(obj);
	}
	//请求数据
	let questData = {
		url: obj.url,
		formData: obj.data,
		name: obj.name,
		fields: obj.fields ||[],
		header: Object.assign({
			"Access-Token": token,
		}, obj.header),
		timeout: obj.timeout || 60000,
		responseFormat: responseFormat,
		errFormat: errFormat,
		dataFormat: "",
		paramUrl: obj.paramUrl,
		resCheck: true,
		resFull: false,
	}
	if(obj.filePath){
		questData["filePath"] = obj.filePath
	}else if(obj.files){
		questData["files"] = obj.files
	}
	//合并接口独立设置
	if(obj.port){
		let portObj;
		let portHeader;
		if($api.isString(obj.port)){
			portObj = host.port[obj.port] ||{}
		}else if($api.isObject(obj.port)){
			portObj = obj.port
		}
		//单独提出header合并处理
		if(portObj.header){
			portHeader = Object.assign({}, questData.header, portObj.header);
		}
		$api.objMerge(questData, portObj);
		if(portHeader){
			questData["header"] = portHeader
		}
	}
	//前置校验
	if(obj.hasOwnProperty("resCheck")){
		questData["resCheck"] = obj.resCheck;
	}
	//完整数据
	if(obj.hasOwnProperty("resFull")){
		questData["resFull"] = obj.resFull;
	}
	if(!questData.url){//无url，返回错误
		if(!obj.noLoading){
			closeLoading(obj);
		}
		//解锁
		if(lock){
			lock["value"] = false
		}
		return Promise.reject({ msg: "url为空", type: "urlErr", from:"uquest", source:obj});
	}
	//参数强制写入url，分两种情况，1、问号开头的参数列表；2、url直接拼接参数
	if(questData.paramUrl){
		if(questData.paramUrl == "splice"){//直接拼接
			questData.url = questData.url + questData.data.data
		}else{
			questData.url = $api.stringParams(questData.data, questData.url);
		}
		delete questData.data;
	}
	//请求字段过滤
	if($api.isArray(questData.fields) && questData.fields.length){
		Object.keys(questData.data).map((item)=>{
			if(!questData.fields.includes(item)){
				delete questData.data[item]
			}
		});
	}
	let questPromise = toPromise(uni.uploadFile)(questData);
	let resPromise = questPromise.then((resData)=>{//数据请求完成
		let errObj, showMsg;
		if(resData.statusCode == 200){//成功，返回包装内层作为原始数据
			let trueData = resData.data;
			//官方目前返回的格式为字符串，需转换
			if(typeof(trueData)=="string"){
				try{
					trueData = JSON.parse(trueData)
				}catch{
					console.error("上传返回结果Json格式化错误");
					trueData ={}
					return Promise.resolve(trueData);
				}
			}
			if(!questData.resCheck){//无需结果校验（也不进行数据格式化），直接返回完整数据
				if(obj.success){
					obj.success(trueData);
				}
				//解锁
				if(lock&&!lock.once){
					lock["value"] = false
				}
				return Promise.resolve(trueData);
			}else{//结果校验
				let errCode = trueData[questData.responseFormat.errCode];
				let errCodeDic = questData.errFormat;
				if($api.isObject(trueData)){
					if(errCode == errCodeDic.success){//校验成功
						if(questData.dataFormat){//数据格式化，如果有
							dataFormat(trueData[questData.responseFormat.data], questData.dataFormat)
						}
						if(obj.success){
							if(questData.resFull){//完整数据
								obj.success(trueData)
							}else{
								obj.success(trueData[questData.responseFormat.data])
							}
						}
						//解锁
						if(lock&&!lock.once){
							lock["value"] = false
						}
						if(questData.resFull){
							return Promise.resolve(trueData);
						}
						return Promise.resolve(trueData[questData.responseFormat.data]);
					}else if(errCode == errCodeDic.serviceErr){//业务错误，带服务器消息
						showMsg = trueData[questData.responseFormat.errMsg];
						errObj = { msg: "业务流错误", type: "serviceErr" };
					}else if(errCode == errCodeDic.loginTimeout){//登录超时
						gotoLogin();
						return Promise.reject({ msg: "登录超时", type: "loginTimeout" });
					}else{
						showMsg = "服务器返回数据错误";
						errObj = { msg: "未定义的错误码或数据定义错误", type: "dataErr" };
					}
				}else{
					showMsg = "服务器返回数据错误";
					errObj = { msg: "返回数据为非对象格式", type: "dataErr" };
				}
				if(errObj){
					errObj["response"] = trueData;
				}
			}
		}else{//失败，判断原因
			showMsg = "http请求异常";
			errObj = { msg: "http状态码异常", type: "httpCodeErr", code:resData.statusCode };
		}
		
		if(!errObj){//未进入成功流且未得到错误对象，检查以上代码是否正确
			showMsg = "http请求出错";
			errObj = { msg: "未定义的http错误，检查http核心文件代码！", type: "codeErr" };
		}
		errObj["from"] = "upload";
		errObj["source"] = obj;
		if(!obj.noErrMsg){
			$api.msg(showMsg, "error");
		}
		if(obj.fail){
			obj.fail();
		}
		//解锁
		if(lock){
			lock["value"] = false
		}
		return Promise.reject(errObj);
	},(err)=>{//失败回调,不应该捕捉success内错误，不使用catch
		if(err && err.type == "cancel"){//取消操作，错误传递
			return Promise.reject(Object.assign(err, { from:"upload", source: obj }));
		}else{
			if(!obj.noErrMsg){
				$api.msg('网络异常', "error");
			}
			if(obj.fail){
				obj.fail();
			}
			return Promise.reject({ from:"upload", msg: "http请求失败", type: "httpErr", source: obj });
		}
	}).finally(()=>{
		if(lock){
			lock["value"] = false
		}
		if(!obj.noLoading){
			closeLoading(obj);
		}
		if(obj.complete){
			obj.complete();
		}
	});
	//请求取消
	resPromise["cancel"] = function(){
		questPromise.cancel();
		questPromise.handle.abort();
	}
	return resPromise
}

/*
对数据进行格式化，通常为值转换，在原数组上处理，同时也返回新数组(浅拷贝)
options:[
	{
		key:"读取键" (存在则当做对象处理，取对应键值)
		writeKey:"写入键" (存在key的前提下，返回值写入对象的键)
		func:"处理类型"
		value:"选项值"
		sign:"正负号" (dateDiffer类型使用，指示结果是否需要正负号)
	},
	……
]
匹配类型func：
	dateFormat:"处理日期" (使用dateFormat处理，返回格式等于value)
	dateDiffer:"日期差值" (使用dateDiffer处理，返回格式等于value)
	Number:"数字化"
	String:"字符化"
	Date:"日期化"
	decimal:"小数化" (小数位数等于value)
	dic:"字典取值" (对象数组可用，字典数据为dicData)
*/
const dataFormat = (targetArr, options=[]) => {
	if($api.typeAsk(options) == "Object"){
		options = [options]
	}
	if($api.typeAsk(targetArr) != "Array"){
		console.error("function: dataFormat, 参数类型错误", {
			"参数1(数组)": $api.typeAsk(targetArr),
		});
	}
	for(let option of options){
		if(option.func == "dic" && $api.typeAsk(option.dicData) == "Function"){//动态生成字典，放在多级循环外
			option.dicData = option.dicData(targetArr)
		}
		for(let i=0; i<targetArr.length; i++){
			let isObj = !!option.key;
			let item = targetArr[i];
			let readKey = option.key;
			let writeKey = option.writeKey || readKey;
			switch(option.func){
				case "dateFormat":
					if(isObj){
						item[writeKey] = $api.dateFormat(item[readKey], {format: option.value})
					}else{
						targetArr[i] = $api.dateFormat(item, {format: option.value})
					}
					break;
				case "dateDiffer":
					if(isObj){
						item[writeKey] = $api.dateDiffer(item[readKey], (option.key1? item[option.key1] :""), {format: option.value, sign: option.sign})
					}else{
						targetArr[i] = $api.dateDiffer(item, {format: option.value, sign: option.sign})
					}
					break;
				case "Number":
					if(isObj){
						item[writeKey] = Number(item[readKey])
					}else{
						targetArr[i] = Number(item)
					}
					break;
				case "String":
					if(isObj){
						item[writeKey] = String(item[readKey])
					}else{
						targetArr[i] = String(item);
					}
					break;
				case "Date":
					if(isObj){
						if(!!Number(item[readKey])){
							item[writeKey] = new Date(Number(item[readKey]))
						}else{
							item[writeKey] = new Date(item[readKey])
						}
					}else{
						if(!!Number(item)){
							targetArr[i] = new Date(Number(item))
						}else{
							targetArr[i] = new Date(item)
						}
					}
					break;
				case "decimal":
					if(isObj){
						item[writeKey] = Number(item[readKey]).fixed(option.value||2)
					}else{
						targetArr[i] = Number(item).fixed(option.value||2)
					}
					break;
				case "dic":
					if(option.dicData){
						let dicDate;
						switch($api.typeAsk(option.dicData)){
							case "String"://读缓存
								dicDate = store.state.dic ? store.state.dic.data[option.dicData] : null;
								break;
							case "Object":
							case "Array":
								dicDate = option.dicData;
								break;
						}
						item[writeKey] = dicDate ? dicDate[item[readKey]] : "没有字典"
					}
					break;
			}
		}
	}
	return [...targetArr]
}

export default{
	appTimeout,
	uploadImageMaxSize,
	toPromise,
	gotoLogin,
	nquest,
	uquest,
	upload
}